#define __DEBUG__
#define THIS_FILE "x_prio_5.sqf"
#include "x_setup.sqf"

__TRACE_1("_this","_this")

private ["_poss","_kind","_objs","_unit","_svec","_isFlat","_crew","_pos","_center_x","_center_y","_count_obj","_radius","_angle","_dir","_angle_plus","_bag"];
PARAMS_2(_poss,_kind);

_type = switch (GVAR(side_enemy)) do {
	case west: { "B_Truck_01_box_F" };
	case resistance: { "I_Truck_02_box_F" };
	default { "O_Truck_02_box_F" };
};

_objs = [];
_unit = createVehicle [_type, _poss, [], 0, "NONE"];
_svec = (sizeOf (typeOf _unit) max 1.5);
_isFlat = (position _unit) isFlatEmpty [_svec / 2, 150, 0.7, _svec, 0, false, _unit];
if (count _isFlat > 1) then {
	_isFlat set [2,0];
	if ((_poss distance _isFlat) < 50) then {
		_poss = _isFlat;
	};
};
_poss set[2,0];
_unit setPos _poss;
_unit lock 2;
_unit setFuel 0;
_unit setFuelCargo 0;
_unit setAmmoCargo 0;

_bag = createVehicle ["CamoNet_OPFOR_big_F",[0,0,0], [], 0, "CAN_COLLIDE"];
_bag allowDamage false;
_bag setPos _poss;
_bag allowDamage true;
_objs set [count _objs, _bag];


_center_x = (getPos _unit) select 0;
_center_y = (getPos _unit) select 1;
_radius = ((12 max _svec) min 14);
_angle = (getDir _unit) + 180;
_count_obj = 3;
_angle_plus = 360 / _count_obj;
for "_i" from 0 to (_count_obj - 1) do {
	_x1 = _center_x - (_radius * sin _angle);
	_y1 = _center_y - (_radius * cos _angle);
	_pos = [_x1,_y1,0];
	_dir = (_angle + 180);
	_angle = _angle + _angle_plus;
	_bag = createVehicle ["Land_HBarrierWall6_F",_pos, [], 0, "CAN_COLLIDE"];
	_bag setDir _dir;
	_bag setPos _pos;
	_objs set [count _objs, _bag];
	
	_pos = _bag modelToWorld [7,0,0];
	_pos set [2,0];
	_bag = createVehicle ["Land_HBarrierWall6_F",_pos, [], 0, "CAN_COLLIDE"];
	_bag setDir _dir;
	_objs set [count _objs, _bag];
	
	_pos = _bag modelToWorld [-14,0,0];
	_pos set [2,0];
	_bag = createVehicle ["Land_HBarrierWall6_F",_pos, [], 0, "CAN_COLLIDE"];
	_bag setDir _dir;
	_objs set [count _objs, _bag];
};
_x1 = _center_x - (_radius * sin (getDir _unit));
_y1 = _center_y - (_radius * cos (getDir _unit));
_pos = [_x1,_y1,0];
_bag = createVehicle ["CamoNet_OPFOR_open_F",_pos, [], 0, "CAN_COLLIDE"];
_bag setDir (getDir _unit + 180);
_bag allowDamage false;
_unit setVariable ["spawn",_bag];

_unit spawn {
	private ["_truck","_players","_center","_radius","_target_array2","_target","_drone","_type","_grp","_wp","_drone_radius","_timeout","_tg_pos", "_fnc_addGunner","_fnc_killed", "_fnc_setTarget","_fnc_createDrone"];
	_truck = _this;
	__TargetInfo
	_center = (_target_array2 select 0);
	_radius = ((_target_array2 select 2) * 2);
	_drone_radius = _radius;
	
	_type = switch (GVAR(side_enemy)) do {
		case west: { "B_UGV_01_rcws_F" };
		case resistance: { "I_UGV_01_rcws_F" };
		default { "O_UGV_01_rcws_F" };
	};
	
	_fnc_addGunner = {
		if (!(isNull (_this getVariable ["gunner",objNull])) && {alive (_this getVariable ["gunner",objNull])}) exitWith {};
		__TRACE_1("addGunner","_this")
		private ["_drone","_unit","_type","_dir"];	
		_drone = _this;
		if !(isNull (_drone getVariable ["gunner",objNull])) then {
			_unit = _drone getVariable ["gunner",objNull];
			detach _unit;
			deleteVehicle _unit;
		};
		_type = switch (GVAR(side_enemy)) do {
			case west: { "B_Soldier_AR_F" };
			case resistance: { "I_Soldier_AR_F" };
			default { "O_Soldier_AR_F" };
		};
		_dir = getdir _drone;
		_unit = (group (driver _drone)) createUnit [_type, [0,0,1000], [], 0, "NONE"];
		_unit allowDamage false;
		_unit hideObjectGlobal true;
		_unit addEventHandler ["HandleDamage",{ 0 }];
		_drone setdir 0;
		_unit setDir 0;
		_unit attachTo [_drone,[0,0,-2.5]];
		_unit setVectorUp [0,0.06,0.1];
		_drone setDir _dir;
		_unit unlinkItem "ItemRadio";
		_drone setVariable ["gunner",_unit];
		__TRACE_1("Added Gunner","_unit")
	};
	
	_fnc_setTarget ={
		private ["_drone","_target","_grp","_gunner","_cnt"];
		PARAMS_2(_drone,_target);
		_grp = group(driver _drone);
		_grp setBehaviour "COMBAT";
		_grp setcombatMode "RED";
		{
			_x reveal [_target, 4];
			__TRACE_3("Reveal","_x","typeOf(_x)","_target")
		} foreach units _grp;
		(gunner _drone) doTarget (vehicle _target);
		(driver _drone) doTarget (vehicle _target);
		sleep 1;
		(driver _drone) doTarget (vehicle _target);
		(gunner _drone) doTarget (vehicle _target);
		sleep 1;
		if !(isNull (_drone getVariable ["gunner",objNull])) then {
			_gunner = (_drone getVariable ["gunner",objNull]);
			_cnt = 4;
			{ if (_x in ["150Rnd_762x51_Box","150Rnd_762x51_Box_Tracer"]) then { __DEC(_cnt);}; 
			} foreach magazines _gunner;
			if (_cnt > 1) then {
				__DEC(_cnt);
				__TRACE_2("Add Magazines","_gunner","_cnt")
				_gunner addMagazines ["150Rnd_762x51_Box", _cnt];
			};
			_gunner doTarget _target;
			sleep 1;
			_gunner doFire _target;
			__TRACE_1("Gunner Target Set","_target")
		};
		sleep 1;
		(driver _drone) doFire (vehicle _target);
		(gunner _drone) doFire (vehicle _target);
		__TRACE_1("Target Set","_target")
	};
	
	_fnc_createDrone = {
		private ["_spawn","_pos","_center","_type","_drone","_grp","_wp","_radius"];
		PARAMS_4(_spawn,_type,_center,_radius);
		_pos = position _spawn;
		_drone = createVehicle [_type,_pos, [], 0, "NONE"];
		_drone setPos (_pos);
		_drone setDir (getDir _spawn);
		createVehicleCrew _drone;
		_drone addEventHandler ["killed",{
			{ 
				detach _x;
				deleteVehicle _x;
			} foreach attachedObjects (_this select 0);
			if !(isNull ((_this select 0) getVariable ["gunner",objNull])) then {
				deleteVehicle ((_this select 0) getVariable ["gunner",objNull]);
			};
			__TRACE("Drone destroyed")
		}];
		_drone lock 2;
		_grp = group (driver _drone);
		_drone setVariable ["group",_grp];
		_grp allowFleeing 0;
		_grp setBehaviour "SAFE";
		_grp setSpeedMode "FULL";
		__addDead(_drone);
#ifdef __DEBUG__	
		GVAR(PRIO_DRONE) = _drone;
		_drone spawn FUNC(AirMarkerMove);
#endif	
		__TRACE_1("Drone Created","_drone")
		_drone;
	};
	
	_drone = objNull;
	_grp = grpNull;
	while { alive _truck } do {
		call FUNC(mpcheck);
		if (!(alive _drone) || {!(canFire _drone)} || {!(canMove _drone)} || {count (crew _drone) <= 0}) then {
			if !(isNull _drone) then {
				{ deleteVehicle _x;
				} foreach units (_drone getVariable "group");
				deleteVehicle _drone;
			};
			_drone = [(_truck getVariable "spawn"), _type, _center, _drone_radius] call _fnc_createDrone;
			[(_drone getVariable "group"), _center, _drone_radius, [0, 0, 0], ""] call FUNC(MakePatrolWPX);
			sleep (60 + random (60));
		} else {
			_grp = (_drone getVariable "group");
			_grp allowFleeing 0;
			if ((_drone distance _center) < _drone_radius) then {
				__TRACE_1("Drone ready to attack","_drone")
				_grp setBehaviour "COMBAT";
				_grp setcombatMode "RED";
				_drone setVehicleAmmo 1;
				_drone setFuel 1;
				_players = if (isMultiPlayer) then { playableUnits } else { [player] };
				for "_i" from 0 to (count _players - 1) do {
					if ((GV((_players select _i),xr_pluncon)) || (vehicle(_players select _i) distance _center) > _radius || (vehicle(_players select _i) distance _truck) < 80) then {
						_players set [_i, objNull];
					};
				};
				_players = _players - [objNull];
				__TRACE_1("Found targets","_players")
				if (count _players > 0) then {
					_target = _players call FUNC(randomArrayVal);
					(driver _drone) doFollow _target;
					[_drone,_target] call _fnc_setTarget;
					_timeout = time + 300;
					_drone flyInHeight 80;
					while { alive _drone && alive _truck && _timeout > time && alive _target && {!(GV(_target,xr_pluncon))} } do {
						_grp setBehaviour "COMBAT";
						_grp setcombatMode "RED";
						(driver _drone) doFollow (position (vehicle _target));
						sleep 10;
					};
					__TRACE_1("Stops following","_target")
				} else {
					(gunner _drone) doWatch objNull;
					sleep (60 + random (60))
				};
			} else {
				__TRACE_1("Drone still moving","_drone")
				_grp setBehaviour "SAFE";
				(driver _drone) doMove _center;
				_grp setSpeedMode "FULL";
			};
			if (alive _drone) then {
				sleep (60 + random (60))
			};
		};
	};
	if !(isNull _drone) then {
		{ deleteVehicle _x;
		} foreach units (_drone getVariable "group");
		_drone setDamage 1;
	};
};

__TRACE_2("End","_unit","_objs");
[_unit,_objs]